package com.emmmya.ocs.config;

import com.emmmya.ocs.config.interceptor.JwtTokenInterceptor;
import com.emmmya.ocs.common.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private ShiroUtil shiroUtils;

    @Bean
    public JwtTokenInterceptor createJwtTokenInterceptor(){
        JwtTokenInterceptor jwtTokenInterceptor =new JwtTokenInterceptor();
        return jwtTokenInterceptor;
    }

    /**
     * i18n中的默认语言
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return slr;
    }
    /**
     * i18n参数
     * @return
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("lang");
        return lci;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //校验没有登录自动转到/login页面
        //registry.addInterceptor(createCheckTokenInterceptor()).addPathPatterns("/**").excludePathPatterns(shiroUtils.getAnon());
        //将jwt拦截器注册到WebMvc里。
        registry.addInterceptor(createJwtTokenInterceptor()).addPathPatterns("/**").excludePathPatterns(shiroUtils.getAnon());
        registry.addInterceptor(localeChangeInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(shiroUtils.getAnon());
    }
}